/**
    Dialog dialogs=new UtiDialogsFactory(ActBarcode.this);
    dialogs.CreateLoadingDialog();弹出
    dialogs.setDialogDismiss();销毁
 */

package com.onemileshop.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class UtiDialogsFactory
{
//   public UtiDialogsFactory(Context _context)
//   {
//	  context = _context;
//   }
//

   private static UtiDialogsFactory factory;
   public static UtiDialogsFactory getInstance()
   {
	  if(factory==null)
		 factory=new UtiDialogsFactory();
	  return factory;
   }

   private AlertDialog dialog;
   private AlertDialog.Builder builder;
   private ProgressDialog mDialog;


   private static final int loadingDialogNoTitle = 0;
   private static final int loadingDialogTitle = 1;

   // private static final int okAlertDialog=0;
   // private static final int okcancelAlertDialog=1;

   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context,String title, String message)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
		 }
	  });
	  builder.create().show();
   }
   
   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context,String title, String message, DialogInterface.OnClickListener listener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定",listener);
	  builder.create().show();
   }

   /**
    * 创建一个带有OK,Cancel按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    * @param OKButtonOnClickListener
    *           OK按钮的监听器
    */
   public void ShowOKCancelAlertDialog(Context context,String title, String message, OnClickListener OKButtonOnClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", OKButtonOnClickListener);
	  builder.setNegativeButton("取消", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			 
			dialog.dismiss();
		 }
	  });
	  builder.create().show();
   }

  /**
   * 创建一个带两个自定义按钮的对话框
   * @param title 标题
   * @param message 正文内容
   * @param LeftBtnString 左侧按钮文字
   * @param LeftBtnOnClickListener 左侧按钮事件
   * @param RightBtnString 右侧按钮文字
   * @param RightBtnOnClickListener 右侧按钮事件
   */
   public void ShowCustomButtonAlertDialog(Context context, String title, String message,
		 String LeftBtnString,OnClickListener LeftBtnOnClickListener,
		 String RightBtnString,OnClickListener RightBtnOnClickListener)
		 
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton(LeftBtnString, LeftBtnOnClickListener);
	  builder.setNegativeButton(RightBtnString, RightBtnOnClickListener);
	  builder.create().show();
   }
   
   public void ShowItemsDialog(Context context,String[] items, DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setItems(items, onClickListener);
	  builder.create().show();
   }

   public void ShowSingleSelectItemDialog(Context context,String[] items, int seletItem ,DialogInterface.OnClickListener onClickListener)
   {
	  if(builder==null)
	  {
		builder = new AlertDialog.Builder(context);
	  }
	  
	  builder.setSingleChoiceItems(items, seletItem, onClickListener);
	  builder.create().show();
   }
   
   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialog(Context context,String title, View view, int height, int width)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }

	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
	  dialog.show();
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialogWithOKCancel(Context context,String title, View view, int height, int width, DialogInterface.OnClickListener clickListener)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.setButton("确定", clickListener);
	  dialog.setButton2("取消", new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			 
			dialog.dismiss();
		 }
	  });
	  dialog.show();
	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
   }

   /**
    * 关闭警告对话框
    */
   public void DismissAlertDialog()
   {
	  try
	  {
		 if (dialog.isShowing())
		 {
			dialog.dismiss();
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissAlertDialog()",e);
	  }

   }
   
   public void ShowToastInfoShort(Context context,String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
   }
   public void ShowToastInfoLong(Context context,String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_LONG).show();
   }
   
   /**
    * 关闭loading对话框
    */
   public void DismissLoadingDialog()
   {
	  try
	  {
		 if (mDialog.isShowing())
		 {
			mDialog.dismiss();
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissLoadingDialog()", e);
	  }

   }
   
   /**
    * 创建一个没有标题栏的loading窗口
    * 
    * @param message
    *           信息
    */
   public void ShowLoadingDialogNoTitle(Context context,String message)
   {
	  CreateLoadingDialog(context,loadingDialogNoTitle, message, null);
   }

   public void ShowLoadingDialog(Context context,String message, String title)
   {
	  CreateLoadingDialog(context,loadingDialogTitle, message, title);
   }



   protected void CreateLoadingDialog(Context context,int id, String message, String title)
   {
	  mDialog = new ProgressDialog(context);
	  switch (id)
	  {
		 case loadingDialogTitle:
			mDialog.setTitle(title);
			mDialog.setMessage(message);
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			break;
		 case loadingDialogNoTitle:
			mDialog.setMessage(message);
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			break;
	  }
	  mDialog.show();
   }
}
